package view;

import util.Controller;
import util.LoopMusicPlayer;
import util.MusicPlayer;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class GameView extends JLayeredPane{

	private JLabel BackButton;
	private JLabel RestartButton;
	private JLabel ReturnButton;
	private JLabel QuitButton;
    private JLabel SunLabel;
    private JLabel Shovel;
    private JPanel ShovelBank;
    private JPanel panel;
    private JPanel Cardboard;
    private JFrame GameFrame;
    private JPanel Menu;
    private JPanel MenuPanel;
    
    private ImageIcon cardboard;
    private ImageIcon shovelBank = new ImageIcon("img\\ShovelBank.png");
    private ImageIcon shovel = new ImageIcon("img\\Shovel.png");
    private ImageIcon shove2 = new ImageIcon("img\\Shovel_hi_res.png");
    private ImageIcon menu = new ImageIcon("img\\Menu_Button1.png");
    private ImageIcon menupanel = new ImageIcon("img\\Menu.png");
    private ImageIcon backbutton = new ImageIcon("img\\Back_Button1.png");
    private ImageIcon restartbutton = new ImageIcon("img\\Restart_Button1.png");
    private ImageIcon returnbutton = new ImageIcon("img\\Return_Button1.png");
    private ImageIcon quitbutton = new ImageIcon("img\\Quit_Button1.png");
    
    private int x = -10;
    private int move = 1;
    private boolean flag = false;
    Plant [][] plants = new Plant[5][9];
    boolean [][] isEmpty = new boolean[5][9];
    private ArrayList<Card> cardList = new ArrayList<>();
    private List<Car> carList = new ArrayList<>();
    private Controller controller;
    


    
    private void chooseCards(){
        cardList.clear();

        Card card1 = new Card("SunFlower", controller);
        card1.setBounds(83, 7, card1.getCardWidth(), card1.getCardHeight());
        Cardboard.add(card1);
        cardList.add(card1);

        Card card2 = new Card("Peashooter", controller);
        card2.setBounds(142, 7, card2.getCardWidth(), card2.getCardHeight());
        Cardboard.add(card2);
        cardList.add(card2);

        Card card3 = new Card("WallNut", controller);
        card3.setBounds(201, 7, card3.getCardWidth(), card3.getCardHeight());
        Cardboard.add(card3);
        cardList.add(card3);

        Card card4 = new Card("CherryBomb", controller);
        card4.setBounds(260, 7, card4.getCardWidth(), card4.getCardHeight());
        Cardboard.add(card4);
        cardList.add(card4);

        Card card5 = new Card("Repeater", controller);
        card5.setBounds(319, 7, card5.getCardWidth(), card5.getCardHeight());
        Cardboard.add(card5);
        cardList.add(card5);

        Card card6 = new Card("WallNutRoll", controller);
        card6.setBounds(378, 7, card6.getCardWidth(), card6.getCardHeight());
        Cardboard.add(card6);
        cardList.add(card6);
    }
    
    
    
    class PaintThread implements Runnable{
        JFrame frame;
        PaintThread(LaunchFrame launchFrame) {
            this.frame = launchFrame;
        }
        @Override
        public void run() {

            Thread bgm = new Thread(new MusicPlayer("audio\\Start.mp3"));
            bgm.start();



            try {
                Thread.sleep(1600);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 2; i <= 300; i++) {
                if (x<=-560&&!flag){
                    move = -1;
                    flag = true;
                    chooseCards();
                    try {
                        Thread.sleep(1800);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    Thread.sleep(4);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                x -= move;
                if (i==15|i==30|i==60|i==285){
                    move += 1;
                }else if (i==150|i==180|i==190){
                    move -= 1;
                }
                panel.repaint();
            }


            for (int i = 1 ,y = -20 ; y <= 5; i++) {
                try {
                    if(y<0) {
                        Cardboard.setBounds(20,y,
                                cardboard.getIconWidth(), cardboard.getIconHeight());
                        ShovelBank.setBounds(465,y,
                                shovelBank.getIconWidth(), shovelBank.getIconHeight());
                        Menu.setBounds(625, y-5, menu.getIconWidth(), menu.getIconHeight());
                    }
                    if (i%2==0){
                        y++;
                    }
                    Thread.sleep(10);
                    ShovelBank.repaint();
                    Cardboard.repaint();
                    Menu.repaint();
                }catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            
            for(int i = 0 ; i<5 ;i++) {
              	 Car car = new Car(i, controller);
              	 new Thread(car).start();
              	 carList.add(car);
            }
       		controller.setCarList(carList);

            JLabel label;
            new Thread(new MusicPlayer("audio\\readysetplant.mp3")).start();
            for (int i=1 ; i<=3 ; i++){
                try {
                    label = new JLabel(new ImageIcon(
                            "img\\PrepareGrowPlants"+i+".png"));
                    GameView.this.add(label,new Integer(5));
                    label.setBounds(250,200,300,200);
                    Thread.sleep(700);
                    label.setVisible(false);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            bgm.stop();

            try {
                Thread.sleep(800);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            controller.checkCards();
            Shovel.setBounds(458, -5, shovel.getIconWidth(), shovel.getIconHeight());
        }
    }



    GameView(LaunchFrame launchFrame, Controller controller){
        this.controller = controller;
        this.GameFrame = launchFrame;
        this.GameFrame.setContentPane(GameView.this);
        this.setVisible(true);
        //this.repaint();

        //背景底板
        ImageIcon background = new ImageIcon("img\\background1.jpg");
        panel = new JPanel(){
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(background.getImage()
                        , x, 0,this.getWidth(), this.getHeight(), this);
            }
        };
        panel.setVisible(false);
        panel.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
        GameView.this.add(panel, new Integer(-1));

        cardboard = new ImageIcon("img\\SeedBank.png");
        Cardboard = new JPanel(){
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(cardboard.getImage()
                        ,0, 0,cardboard.getIconWidth(), cardboard.getIconHeight(), this);
            }
        };
        Cardboard.setOpaque(false);
        Cardboard.setVisible(true);
        Cardboard.setLayout(null);
        GameView.this.add(Cardboard, new Integer(1));   //选择木板


        ShovelBank = new JPanel(){
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(shovelBank.getImage()
                        ,0, 0,shovelBank.getIconWidth(), shovelBank.getIconHeight(), this);
            }
        };
        ShovelBank.setOpaque(false);
        ShovelBank.setVisible(true);
        GameView.this.add(ShovelBank, new Integer(1));


        Shovel = new JLabel(shovel);
        GameView.this.add(Shovel, new Integer(11));
        Shovel.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                Shovel.setVisible(false);
                launchFrame.setCursor(Frame.DEFAULT_CURSOR);
            }

            public void mouseEntered(MouseEvent e) {
                launchFrame.setCursor(Frame.HAND_CURSOR);
            }

            public void mouseExited(MouseEvent e) {
                launchFrame.setCursor(Frame.DEFAULT_CURSOR);
            }
        });
        

        /*        菜单                    */
        MenuPanel = new JPanel(){
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(menupanel.getImage()
                        ,(800-menupanel.getIconWidth()) / 2, (600-menupanel.getIconHeight()) / 2,menupanel.getIconWidth(), menupanel.getIconHeight(), this);
            }
        };
        MenuPanel.setBounds(0, 0,800, 600);
        MenuPanel.setOpaque(false);
        MenuPanel.setVisible(false);
        MenuPanel.setLayout(null);
        MenuPanel.addMouseListener(new MouseAdapter() {});
        GameView.this.add(MenuPanel, new Integer(10000));
        
        Menu = new JPanel(){
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(menu.getImage()
                        ,0, 0,menu.getIconWidth(), menu.getIconHeight(), this);
            }
        };
        Menu.setOpaque(false);
        Menu.setVisible(true);
        GameView.this.add(Menu, new Integer(1));
        Menu.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				menu = new ImageIcon("img\\Menu_Button2.png");
				Menu.repaint();
				launchFrame.setCursor(Frame.HAND_CURSOR);
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				menu = new ImageIcon("img\\Menu_Button3.png");
				Menu.repaint();
				launchFrame.setCursor(Frame.HAND_CURSOR);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				menu = new ImageIcon("img\\Menu_Button1.png");
				Menu.repaint();
				launchFrame.setCursor(Frame.DEFAULT_CURSOR);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
			menu = new ImageIcon("img\\Menu_Button2.png");
			Menu.repaint();
		        launchFrame.setCursor(Frame.HAND_CURSOR);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				controller.setGameState("PAUSE");
				MenuPanel.setVisible(true);
				launchFrame.setCursor(Frame.HAND_CURSOR);
			}
		});
        /*   返回游戏                 */
        BackButton = new JLabel() {
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(backbutton.getImage()
                        ,0, 0,backbutton.getIconWidth(), backbutton.getIconHeight(), this);
            }
        };
        MenuPanel.add(BackButton);
        BackButton.setBounds(225,445, backbutton.getIconWidth(), backbutton.getIconHeight());
        BackButton.setVisible(true);
        BackButton.addMouseListener(new MouseAdapter() {
		
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
			   backbutton = new ImageIcon("img\\Back_Button2.png");
				BackButton.repaint();
				launchFrame.setCursor(Frame.HAND_CURSOR);
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				backbutton = new ImageIcon("img\\Back_Button3.png");
				BackButton.repaint();
				launchFrame.setCursor(Frame.DEFAULT_CURSOR);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				backbutton = new ImageIcon("img\\Back_Button1.png");
				BackButton.repaint();
				launchFrame.setCursor(Frame.DEFAULT_CURSOR);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				backbutton = new ImageIcon("img\\Back_Button2.png");
				BackButton.repaint();
				launchFrame.setCursor(Frame.HAND_CURSOR);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				MenuPanel.setVisible(false);
				launchFrame.setCursor(Frame.HAND_CURSOR);
			}
		});
        /*  重新开始          */
            RestartButton = new JLabel() {
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(restartbutton.getImage()
                        ,0, 0,restartbutton.getIconWidth(), restartbutton.getIconHeight(), this);
            }
        };
        MenuPanel.add(RestartButton);
        RestartButton.setBounds(280,220, restartbutton.getIconWidth(), restartbutton.getIconHeight());
        RestartButton.setVisible(true);
        RestartButton.addMouseListener(new MouseAdapter() {
		
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
			restartbutton = new ImageIcon("img\\Restart_Button2.png");
			   RestartButton.repaint();
				launchFrame.setCursor(Frame.HAND_CURSOR);
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				restartbutton = new ImageIcon("img\\Restart_Button3.png");
				RestartButton.repaint();
				launchFrame.setCursor(Frame.DEFAULT_CURSOR);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				restartbutton = new ImageIcon("img\\Restart_Button1.png");
				RestartButton.repaint();
				launchFrame.setCursor(Frame.DEFAULT_CURSOR);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				restartbutton = new ImageIcon("img\\Restart_Button2.png");
				RestartButton.repaint();
				launchFrame.setCursor(Frame.HAND_CURSOR);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
				launchFrame.setCursor(Frame.HAND_CURSOR);
			}
		});
        /*    返回主菜单   */
        ReturnButton = new JLabel() {
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(returnbutton.getImage()
                        ,0, 0,returnbutton.getIconWidth(), returnbutton.getIconHeight(), this);
            }
        };
        MenuPanel.add(ReturnButton);
        ReturnButton.setBounds(280,290, returnbutton.getIconWidth(), returnbutton.getIconHeight());
        ReturnButton.setVisible(true);
        ReturnButton.addMouseListener(new MouseAdapter() {
		
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				returnbutton = new ImageIcon("img\\Return_Button2.png");
				ReturnButton.repaint();
				launchFrame.setCursor(Frame.HAND_CURSOR);
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				returnbutton = new ImageIcon("img\\Return_Button3.png");
				ReturnButton.repaint();
				launchFrame.setCursor(Frame.DEFAULT_CURSOR);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				returnbutton = new ImageIcon("img\\Return_Button1.png");
				ReturnButton.repaint();
				launchFrame.setCursor(Frame.DEFAULT_CURSOR);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				returnbutton = new ImageIcon("img\\Return_Button2.png");
				ReturnButton.repaint();
				launchFrame.setCursor(Frame.HAND_CURSOR);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
				launchFrame.setCursor(Frame.HAND_CURSOR);
			}
		});
        
        /*    退出这个游戏   */
        QuitButton = new JLabel() {
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(quitbutton.getImage()
                        ,0, 0,quitbutton.getIconWidth(), quitbutton.getIconHeight(), this);
            }
        };
        MenuPanel.add(QuitButton);
        QuitButton.setBounds(280,360, quitbutton.getIconWidth(), quitbutton.getIconHeight());
        QuitButton.setVisible(true);
        QuitButton.addMouseListener(new MouseAdapter() {
		
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				quitbutton = new ImageIcon("img\\Quit_Button2.png");
				QuitButton.repaint();
				launchFrame.setCursor(Frame.HAND_CURSOR);
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				quitbutton = new ImageIcon("img\\Quit_Button3.png");
				QuitButton.repaint();
				launchFrame.setCursor(Frame.DEFAULT_CURSOR);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				quitbutton = new ImageIcon("img\\Quit_Button1.png");
				QuitButton.repaint();
				launchFrame.setCursor(Frame.DEFAULT_CURSOR);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				quitbutton = new ImageIcon("img\\Quit_Button2.png");
				QuitButton.repaint();
				launchFrame.setCursor(Frame.HAND_CURSOR);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
				launchFrame.setCursor(Frame.HAND_CURSOR);
			}
		});
        
        SunLabel = new JLabel(""+50,JLabel.CENTER);
        SunLabel.setBounds(20, 60, 35, 20);
        Cardboard.add(SunLabel);

        controller.setCardList(cardList);
        controller.setLayeredPane(this);
        controller.setFrame(launchFrame);
        controller.setSunCount(SunLabel);


        Executor exec = Executors.newSingleThreadExecutor();


        if (controller.isNewGame()) {
            panel.setVisible(true);
            Thread Animation = new Thread(new PaintThread(launchFrame));
            exec.execute(Animation);
        }else {
            x = -215;
            Cardboard.setBounds(20,5,
                    cardboard.getIconWidth(), cardboard.getIconHeight());
            ShovelBank.setBounds(465,5,
                    shovelBank.getIconWidth(), shovelBank.getIconHeight());
            panel.repaint();
            panel.setVisible(true);
            chooseCards();
            controller.checkCards();
            Shovel.setBounds(458, -5, shovel.getIconWidth(), shovel.getIconHeight());
        }

		
        Thread bgm = new Thread(new LoopMusicPlayer("audio\\UraniwaNi.mp3"));
        exec.execute(bgm);
            	
		controller.setCarList(carList);

        Thread ZombieThread = new Thread(new ZombieFactory(controller));
        ZombieThread.start();

        Thread SunThread = new Thread(new SunFactory(this, launchFrame, SunLabel, controller));
        SunThread.start();

        Thread PlantThread = new Thread(new PlantFactory(controller));
        PlantThread.start();
                
        JPanel topPanel = controller.getTopPanel();
        topPanel.setVisible(false);
        topPanel.setOpaque(false);
        topPanel.setBounds(0,0, background.getIconWidth(), background.getIconHeight());
        GameView.this.add(topPanel, new Integer(999));

    }
}
